import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;



public class Calculator extends JFrame implements ActionListener{
	
	
	// variable declarations
	
	//variable declarations
	private int n;
	private String c,s1,s2,s3,s4,s5;
	private JLabel jlbOutput;
	private JButton jbnButtons[];
	private JPanel jplMaster, jplBackSpace, jplControl;
	
	/*
	 * Font(String name, int style, int size)
      Creates a new Font from the specified name, style and point size.
	 */
	
	Font f12 = new Font("Times New Roman", 0, 12);
	Font f121 = new Font("Times New Roman", 1, 12);
	
	// Constructor 
	
	public Calculator() {
		
		setBackground(Color.gray);

		jplMaster = new JPanel();

		jlbOutput = new JLabel("");
		jlbOutput.setHorizontalTextPosition(JLabel.RIGHT);
		jlbOutput.setBackground(Color.WHITE);
		jlbOutput.setOpaque(true);
		
		// Add components to frame
				getContentPane().add(jlbOutput, BorderLayout.NORTH);
				
				jbnButtons = new JButton[16];
//				GridLayout(int rows, int cols, int hgap, int vgap) 

				JPanel jplButtons = new JPanel();			// container for Jbuttons

				// Create numeric Jbuttons
				for (int i=0; i<=9; i++)
				{
					// set each Jbutton label to the value of index
					jbnButtons[i] = new JButton(String.valueOf(i));
				}	
				
				// Create operator Jbuttons
				
				jbnButtons[10] = new JButton("=");
				jbnButtons[11] = new JButton("/");
				jbnButtons[12] = new JButton("*");
				jbnButtons[13] = new JButton("-");
				jbnButtons[14] = new JButton("+");
				
				
				jplControl = new JPanel();
				jplControl.setLayout(new GridLayout(1, 2, 2 ,2));

				jbnButtons[15] = new JButton("C");

				jplControl.add(jbnButtons[15]);
				
				
//				Setting all Numbered JButton's to Blue. The rest to Red
				for (int i=0; i<jbnButtons.length; i++)	{
					jbnButtons[i].setFont(f12);

					if (i<10)
						jbnButtons[i].setForeground(Color.blue);
						
					else
						jbnButtons[i].setForeground(Color.red);
				}
				
				
				// Set panel layout manager for a 4 by 5 grid
				jplButtons.setLayout(new GridLayout(4, 4, 2, 2));
				
				//Add buttons to keypad panel starting at top left
				// First row
				for(int i=7; i<=9; i++)		{
					jplButtons.add(jbnButtons[i]);
				}
				
				// add button / 
				jplButtons.add(jbnButtons[11]);
			

				// Second row
				for(int i=4; i<=6; i++)
				{
					jplButtons.add(jbnButtons[i]);
				}
				
				// add button * 
				jplButtons.add(jbnButtons[12]);
				
				
				// Third row
				for( int i=1; i<=3; i++)
				{
					jplButtons.add(jbnButtons[i]);
				}
				
				//adds button - 
				jplButtons.add(jbnButtons[13]);
				
				//Fourth Row
				// add 0, +, and =
				jplButtons.add(jbnButtons[0]);
				jplButtons.add(jbnButtons[14]);
				jplButtons.add(jbnButtons[10]);
				
				
				jplMaster.setLayout(new BorderLayout());
				jplMaster.add(jplControl, BorderLayout.EAST);
				jplMaster.add(jplButtons, BorderLayout.SOUTH);
				
				// Add components to frame
				getContentPane().add(jplMaster, BorderLayout.SOUTH);
				requestFocus();
				
				//activate ActionListener
				for (int i=0; i<jbnButtons.length; i++){
					jbnButtons[i].addActionListener(this);
				}
				
				//add WindowListener for closing frame and ending program
				addWindowListener(new WindowAdapter() {

						public void windowClosed(WindowEvent e)
						{
							System.exit(0);
						}
					}
				);
				this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				
	} //End of Contructor Calculator
	
	// Perform action
	public void actionPerformed(ActionEvent e){
		
		// Search for the button pressed until end of array or key found
				for (int i=0; i<jbnButtons.length; i++)
				{
					if(e.getSource() == jbnButtons[i])
					{
						switch(i)
						{
							case 0:
								addDigitToDisplay(i);
								break;

							case 1:
								addDigitToDisplay(i);
								break;

							case 2:
								addDigitToDisplay(i);
								break;

							case 3:
								addDigitToDisplay(i);
								break;

							case 4:
								addDigitToDisplay(i);
								break;

							case 5:
								addDigitToDisplay(i);
								break;

							case 6:
								addDigitToDisplay(i);
								break;

							case 7:
								addDigitToDisplay(i);
								break;

							case 8:
								addDigitToDisplay(i);
								break;

							case 9:
								addDigitToDisplay(i);
								break;

							case 10:	// =
								processEquals();
								break;

							case 11:	// /
								processOperator("/");
								break;

							case 12:	// *
								processOperator("*");
								break;

							case 13:	// -
								processOperator("-");
								break;

							case 14:	// +
								processOperator("+");
								break;

							case 15:	// C
								clearAll();
								break;

							}
					}
				}
	}
	
	
	void addDigitToDisplay(int digit){
		s3=jlbOutput.getText();
		s4=Integer.toString(digit);
		s5=s3+s4;
		jlbOutput.setText(s5); 
	}
	
	void processOperator(String op) {
						
				s1=jlbOutput.getText();
				jlbOutput.setText("");
				c=op;
	}
	
	
	void processEquals(){
		
		s2=jlbOutput.getText();
		//System.out.println("c value is"+c);
		if (c.equals("+")){
			
		//	System.out.println("plus loop executed");
			n=Integer.parseInt(s1)+Integer.parseInt(s2);
			jlbOutput.setText(String.valueOf(n));
		}
		else if (c.equals("-")){
			n=Integer.parseInt(s1)-Integer.parseInt(s2);
			jlbOutput.setText(String.valueOf(n));
		}
		else if (c.equals("*")){
			n=Integer.parseInt(s1)*Integer.parseInt(s2);
			jlbOutput.setText(String.valueOf(n));
		}
		else if (c.equals("/")){
			n=Integer.parseInt(s1)/Integer.parseInt(s2);
			jlbOutput.setText(String.valueOf(n));
		}
	}		
	
	void clearAll(){
		
		jlbOutput.setText("");
	}
	

	
	
	
	public static void main(String args[]) {
		Calculator calci = new Calculator();
		Container contentPane = calci.getContentPane();
//		contentPane.setLayout(new BorderLayout());
		calci.setTitle("Java Swing Calculator");
		calci.setSize(241, 237);
		calci.pack();
		calci.setLocation(400, 250);
		calci.setVisible(true);
		calci.setResizable(false);
		
	}
	
}		//End of Swing Calculator Class.
	
	
	
	
	

